# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2JsonByYM
from com.zzh.utils.pagingUtils import MyPaging
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

import time
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


class OrderLogAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def addOrderLog(self, orderLog_info):
        orderId = orderLog_info['orderId']
        userId = orderLog_info['userId']
        orderActionCode = orderLog_info['orderActionCode']
        orderActionDate = orderLog_info['orderActionDate']
        orderRemark = orderLog_info['orderRemark']

        sql = "insert into zzh_order_log (order_id,user_id,order_action_code,order_action_date,order_remark) VALUES (%s,%s,%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [orderId, userId, orderActionCode, orderActionDate, orderRemark])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectOrderLog(self, id):
        """
        根据ID查询信息，返回元组

        """
        sql = "select * from zzh_order_log WHERE order_log_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    def updateOrderLogById(self, newUpdateOrderLog, orderLogId):
        orderId = newUpdateOrderLog['orderId']
        userId = newUpdateOrderLog['userId']
        orderActionCode = newUpdateOrderLog['orderActionCode']
        orderActionDate = newUpdateOrderLog['orderActionDate']
        orderRemark = newUpdateOrderLog['orderRemark']
        cur = self.conn.cursor()
        sql = "UPDATE zzh_order_log SET order_id=%s,user_id=%s,order_action_code=%s,order_action_date=%s,order_remark=%s where order_log_id=%s"
        updateOrderLogResultOfDatabase = cur.execute(sql,
                                                     [orderId, userId, orderActionCode, orderActionDate, orderRemark,
                                                      orderLogId])
        self.conn.commit()
        if updateOrderLogResultOfDatabase:
            cur.close()
            self.conn.close()
            return 1
        else:
            cur.close()
            self.conn.close()
            return 0

    def queryOrderLog(self, order_id):
        cur = self.conn.cursor()
        sql = "select * from zzh_order_log WHERE order_log_id=%s"
        total_count = cur.execute(sql, [order_id])
        if total_count:
            OrderLogList = cur.fetchall()
            OrderLogListJson = Table2JsonByYM.orderLog_list_to_json(OrderLogList)
            cur.close()
            self.conn.close()
            return total_count, OrderLogListJson
        else:
            return 0,None

    def queryPointItemsByCondition(self,**kwargs):
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        userId = kwargs['userId']
        cur = self.conn.cursor()
        if userId is None :
            sql = "SELECT * FROM zzh_order_log WHERE user_id=%s "
            total_count = cur.execute(sql, [userId])

        else:
            sql = "SELECT * FROM zzh_order_log"
            total_count = cur.execute(sql)


        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            resultList= MyPaging().getPage(total_count,pageIndex,pageSize,result)

            userOauthJSON = Table2JsonByYM.point_log_listto_json(resultList)
            return total_count, userOauthJSON
        else:
            cur.close()
            self.conn.close()
            return None

    def deleteOrderLogById(self, ids):
        cur = self.conn.cursor()
        sql = "DELETE from zzh_order_log WHERE order_log_id=%s"
        for id in ids:
            deleteOrderLogResultOfDatabase = cur.execute(sql, [id])
        self.conn.commit()
        if deleteOrderLogResultOfDatabase:
            cur.close()
            self.conn.close()
            return 1
        else:
            cur.close()
            self.conn.close()
            return 0
